
package controllers.classes;

import classes.HibernateUtil;
import dao.EmployeeDao;
import dao.PageDao;
import dao.RoleDao;
import dao.StudentDao;
import dao.UserDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Employee;
import models.Page;
import models.Role;
import models.Student;
import models.User;
import org.hibernate.Session;

/**
 * This servlet is the controller that handles the post for the install.jsp
 * Creates an admin account and saves it
 * Also creates the static pages and saves them
 * 
 * @author Achie
 */
public class InstallController extends HttpServlet {

    /**
     * Gets the information from the form and saves them.
     * Saves an admin account and static pages.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //connection to the database using dao
        HibernateUtil hu = new HibernateUtil();
        UserDao ud = new UserDao(hu.openSession());

        if (ud.findAll().size() > 0) { //it is already installed, stop executing the method.
            hu.close(); //close connection
            return;
        }

        insertAdmin(request, response);
        insertPages(request, response);

        hu.close(); //close connection

        //after installing the application, send to the index page
        response.sendRedirect("Index");
    }


    //Inserts standard pages in the database.
    private void insertPages(HttpServletRequest request, HttpServletResponse response) {
        HibernateUtil hu = new HibernateUtil();
        Session sessie = hu.openSession();
        PageDao pdao = new PageDao(sessie);

        //Standard content for each page.
        String content = "This content is currently empty please edit content in the manage pages.";

        //Make the pages.
        Page homepage = new Page(0, "Homepage", content);
        Page membership = new Page(0, "Membership", content);
        Page about = new Page(0, "About", content);
        Page contact = new Page(0, "Contact", content);
        Page help = new Page(0, "Help", content);
        Page privacypolicy = new Page(0, "Privacy policy", content);
        Page disclaimer = new Page(0, "Disclaimer", content);


        //Save pages into database.
        pdao.save(homepage);
        pdao.save(membership);
        pdao.save(about);
        pdao.save(contact);
        pdao.save(help);
        pdao.save(privacypolicy);
        pdao.save(disclaimer);

        hu.close();

    }

    /*
     * this method inserts the first admin (head administrator)
     */
    private void insertAdmin(HttpServletRequest request, HttpServletResponse response) {
        HibernateUtil hu = new HibernateUtil();
        Session session = hu.openSession();

        RoleDao rd = new RoleDao(session);

        long id = System.nanoTime();
        Role administrator = new Role(id, "Administrator"); //Create administrator role
        rd.save(administrator); //save administrator role

        Role teacher = new Role(id, "Teacher"); //Create teacher role
        rd.save(teacher); //save teacher role

        //grab the parameters from the forum
        long userid = System.nanoTime();
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String firstname = request.getParameter("firstname");
        String insertion = request.getParameter("insertion");
        String lastname = request.getParameter("lastname");
        String description = request.getParameter("description");

        //Get role from database
        List<Role> rollenlijst = rd.findAll();
        Role temprol = null;
        for (int i = 0; i < rollenlijst.size(); i++) {
            if (rollenlijst.get(i).getName().equals("Administrator")) {
                temprol = rollenlijst.get(i);
            }
        }

        //New user object
        User user = new User(userid, email, password, firstname, insertion, lastname);
        user.setDate();
        UserDao ud = new UserDao(session);
        ud.save(user);

        //Create employee object and assign the role from the database
        Employee employee = new Employee(userid, user, temprol, firstname, lastname, insertion, description);
        employee.setRemovable(false);
        EmployeeDao ed = new EmployeeDao(session);
        ed.save(employee);
    }

    /*
     * this method created a dummy student account
     */
    private void insertStudent(HttpServletRequest request, HttpServletResponse response) {
        HibernateUtil hu = new HibernateUtil();
        Session sessie = hu.openSession();

        StudentDao sdao = new StudentDao(sessie);
        UserDao udao = new UserDao(sessie);

        long id = System.nanoTime();
        User user = new User(id, "Student@32learn.nl", "123456", "[dummy]Student", "", "account");
        //Save and make student
        udao.save(user);
        sdao.save(new Student(id, user, "[dummy]Student", "", "Account", ""));

        hu.close();
    }
}
